const utils = require("./utils.js");
let appglobal = require("../../appglobal.js");
class datacontext {
    constructor() {
        this.znodes = [];
        this.defaultProps = {
            children: "children",
            label: "label"
        }
    }
    assemblytree(dataparm) {
        if (!dataparm || dataparm.code != '200' || dataparm.data.length == 0)
            return;
        this.znodes = [];
        let treeJson = dataparm.data[0];
        let { dirInfoList, docTypeList, docList, fileList } = treeJson.viewOutInfo;
        dirInfoList.forEach(dir => {
            if (!dirInfoList.find(d => dir.parentTemplateDirId === d.templateDirId)) {
                this.znodes.push(this.getTreeNodeDir(dirInfoList, docTypeList, docList, fileList, dir));
            }
        });
        this.znodes.forEach(n => { this.setNodesImagesCount(n) });
    }
    //递归获取目录节点
    getTreeNodeDir(dirInfoList, docTypeList, docList, fileList, dir) {
        let treeNode = dir;
        treeNode.label = dir.templateDirName;
        treeNode.children = [];
        treeNode.levelTree = "dir";
        treeNode.key = dir.templateDirId;
        let childrenDir = dirInfoList.filter(d => {
            return d.parentTemplateDirId == dir.templateDirId;
        });
        if (childrenDir.length > 0) {
            childrenDir.forEach(element => treeNode.children.push(this.getTreeNodeDir(dirInfoList, docTypeList, docList, fileList, element)));
        }
        //若目录下子目录为空,就是叶子目录了,然后获取文档类型列表
        else {
            let docTypeArr = docTypeList.filter(docType => docType.dirId === dir.templateDirId)
            if (docTypeArr.length > 0) {
                docTypeArr.forEach(docType => {
                    let treeNodeType = docType;
                    treeNodeType.label = docType.docTypeName;
                    treeNodeType.children = [];
                    treeNodeType.levelTree = "docType";
                    treeNodeType.key = docType.bizGuId + "#" + docType.docTypeCode;
                    treeNode.children.push(treeNodeType);
                    //加载文档列表
                    let docArr = docList.filter(doc => doc.docType == docType.docTypeCode && docType.bizGuId == doc.guid);
                    if (docArr.length > 0) {
                        docArr.forEach(doc => {
                            let treeNodeDoc = doc;
                            treeNodeDoc.label = doc.docName;
                            treeNodeDoc.levelTree = "doc";
                            treeNodeDoc.refDocType = docType;
                            treeNodeDoc.key = doc.docId;
                            //挂载图片列表数据
                            treeNodeDoc.images = fileList.filter(f => f.docId === doc.docId);
                            treeNodeType.children.push(treeNodeDoc);
                        });
                    }
                });
            }
        }
        return treeNode;
    }
    //设置所有节点挂载的图片数量
    setNodesImagesCount(node) {
        return node.children ? node.imagesCount = node.children.reduce((s, d) => {
            return s + this.setNodesImagesCount(d);
        }, 0) : node.imagesCount = node.images.length;
    }
    //获取节点上的所有图片分组信息和附件信息
    getNodeContent(data, imageList, attachFileList) {
        data = data || {};
        if (data.levelTree != "doc") {
            let allNodes = [];
            this.getDocNodes(data, allNodes);
            allNodes.forEach(e => {
                this.getNodeContent(e, imageList, attachFileList);
            });
        }
        else {
            if (data.images) {
                let imageItems = [];
                data.images.forEach(e => {
                    if (!e.newadd)
                        e.url = `${appglobal.serverApiUrl}/file/show?appId=${appglobal.appId}&fileId=${e.fileId}`;
                    utils.isImage(e.fileName) ? imageItems.push(e) : attachFileList.push(e);
                });
                if (imageItems.length > 0) {
                    let imgGroup = imageList.find(g => g.groupTitle == data.refDocType.label);
                    imgGroup ? imgGroup.items.unshift(...imageItems) : imageList.push({ groupTitle: data.refDocType.label, items: imageItems });
                }
            }
        }
    }
    //获取要上传的文件列表
    getUploadFiles(data, files) {
        data = data || {};
        if (data.levelTree != "doc") {
            let allNodes = [];
            this.getDocNodes(data, allNodes);
            allNodes.forEach(e => {
                this.getUploadFiles(e, files);
            });
        }
        else {
            if (data.images) {
                data.images.forEach(e => {
                    if (e.newadd)
                        files.push(e);
                });
            }
        }
    }
    //获取特定节点的整个数据,dir,doctype,循环次数过多影响性能,但可以忽略
    getNodes(data, levelTree, returnNodes) {
        returnNodes = returnNodes || [];
        if (data.levelTree === levelTree) {
            returnNodes.push(data);
        }
        if (data.children) {
            data.children.forEach(d => {
                this.getNodes(d, levelTree, returnNodes);
            });
        }
    }
    getDocNodes(data, returnNodes) {
        return this.getNodes(data, "doc", returnNodes)
    }
    getDocTypeNodes(data, returnNodes) {
        return this.getNodes(data, "docType", returnNodes)
    }
    getDirNodes(data, returnNodes) {
        return this.getNodes(data, "dir", returnNodes)
    }
}
module.exports = new datacontext();